package com.wang.controller;

import com.wang.pojo.User;
import com.wang.service.UserService;
import com.wang.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userServiceimpl;

    @GetMapping("/queryAllUser")
    public String queryAllUser(HttpServletRequest request){
        List<User> users = userServiceimpl.queryUser();
        request.setAttribute("users", users);
        return "listUser";
    }
    @GetMapping("/turnAddUser")
    public String turnAddUser(){
        return "addUser";
    }
    @GetMapping("/addUser")
    public String addUser(User user){
        int i = userServiceimpl.addUser(user);
        return "forward:/queryAllUser";
    }
    @GetMapping("/queryUserById/{id}")
    public String queryUserById(@PathVariable("id") Integer id, Model model){
        User user = userServiceimpl.queryUserById(id);
        model.addAttribute("user", user);
        return "updateUser";
    }
    @GetMapping("/updateUser")
    public String updateUser(User user){
        userServiceimpl.updateUser(user);
        return "forward:/queryAllUser";
    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUserById(@PathVariable("id")Integer id){
        userServiceimpl.deleteUserById(id);
        return "forward:/queryAllUser";
    }
}
